home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / text / cmanual.lzh / ACM2.lzh / Menus / Example3.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  18KB  |  432 lines

  1. /* Example3                                                             */
  2. /* This program opens a normal window to which we connect a menu strip. */
  3. /* The menu will look like this:                                        */
  4. /*                                                                      */
  5. /* Edit                                                                 */
  6. /* ----------                                                           */
  7. /* | Style -----------------------                                      */
  8. /* --------| v Plain       [A] P |                                      */
  9. /*         |   Bold        [A] B |                                      */
  10. /*         |   Underlined  [A] U |                                      */
  11. /*         |   Italic      [A] I |                                      */
  12. /*         -----------------------                                      */
  13. /*                                                                      */
  14. /* This example is very similar to Example2, but the user can this time */
  15. /* also access the subitems from the keyboard. For example, to select   */
  16. /* Bold the user only needs to press the right Amiga key [A] together   */
  17. /* with the "B" key. */
  18.  
  19.  
  20.  
  21. #include <intuition/intuition.h>
  22.  
  23.  
  24.  
  25. struct IntuitionBase *IntuitionBase;
  26.  
  27.  
  28.  
  29. /*************************************************************************/
  30. /*                      F O U R T H   S U B I T E M                      */
  31. /*************************************************************************/
  32.  
  33. /* The text for the fourth subitem: */
  34. struct IntuiText my_fourth_text=
  35. {
  36.   2,            /* FrontPen, black. */
  37.   0,            /* BackPen, not used since JAM1. */
  38.   JAM1,         /* DrawMode, do not change the background. */
  39.   CHECKWIDTH,   /* LeftEdge, CHECKWIDTH amount of pixels out. */
  40.                 /* This will leave enough space for the check mark. */
  41.   1,            /* TopEdge, 1 line down. */
  42.   NULL,         /* TextAttr, default font. */
  43.   "Italic",     /* IText, the string. */
  44.   NULL          /* NextItem, no link to other IntuiText structures. */
  45. };
  46.  
  47. /* The MenuItem structure for the fourth subitem: */
  48. struct MenuItem my_fourth_subitem=
  49. {
  50.   NULL,            /* NextItem, this is the last subitem in the list. */
  51.   50,              /* LeftEdge, 50 pixels out. */
  52.   35,              /* TopEdge, 35 lines down. */
  53.   150,             /* Width, 150 pixels wide. */
  54.                    /*        150 pixels is enough in this example (the */
  55.                    /*        Amiga key + character fits perfectly), but */
  56.                    /*        if you are not sure you can always add the */
  57.                    /*        constant COMMWIDTH. Eg, 150 + COMMWIDTH. */
  58.   10,              /* Height, 10 lines high. */
  59.   ITEMTEXT|        /* Flags, render this item with text. */
  60.   ITEMENABLED|     /*        this item will be enabled. */
  61.   CHECKIT|         /*        it is an attribute item. */
  62.   COMMSEQ|         /*        also accessable from the keyboard. */
  63.   HIGHCOMP,        /*        complement the colours when highlihted. */
  64.   0x00000001,      /* MutualExclude, mutualexclude the first subitem. */
  65.   (APTR) &my_fourth_text, /* ItemFill, pointer to the text. */
  66.   NULL,            /* SelectFill, nothing since we complement the col. */
  67.   'I',             /* Command, the user can select this item by      */
  68.                    /*          pressing the right Amiga key together */
  69.                    /*          with the I key. Remember to:          */ 
  70.                    /*          1. Set the flag COMMSEQ.              */
  71.                    /*          2. Make the itembox wide enough.      */
  72.                    /*          (Intuition does not care if you write */
  73.                    /*          a capital letter or not. Pressing the */
  74.                    /*          Amiga key together with an 'I' or an  */
  75.                    /*          'i' makes no difference.)             */
  76.   NULL,            /* SubItem, ignored by Intuition. */
  77.   MENUNULL,        /* NextSelect, no items selected. */
  78. };
  79.  
  80.  
  81.  
  82. /*************************************************************************/
  83. /*                       T H I R D   S U B I T E M                       */
  84. /*************************************************************************/
  85.  
  86. /* The text for the third subitem: */
  87. struct IntuiText my_third_text=
  88. {
  89.   2,            /* FrontPen, black. */
  90.   0,            /* BackPen, not used since JAM1. */
  91.   JAM1,         /* DrawMode, do not change the background. */
  92.   CHECKWIDTH,   /* LeftEdge, CHECKWIDTH amount of pixels out. */
  93.                 /* This will leave enough space for the check mark. */
  94.   1,            /* TopEdge, 1 line down. */
  95.   NULL,         /* TextAttr, default font. */
  96.   "Underlined", /* IText, the string. */
  97.   NULL          /* NextItem, no link to other IntuiText structures. */
  98. };
  99.  
  100. /* The MenuItem structure for the third subitem: */
  101. struct MenuItem my_third_subitem=
  102. {
  103.   &my_fourth_subitem, /* NextItem, linked to the fourth subitem. */
  104.   50,              /* LeftEdge, 50 pixels out. */
  105.   25,              /* TopEdge, 25 lines down. */
  106.   150,             /* Width, 150 pixels wide. */
  107.   10,              /* Height, 10 lines high. */
  108.   ITEMTEXT|        /* Flags, render this item with text. */
  109.   ITEMENABLED|     /*        this item will be enabled. */
  110.   CHECKIT|         /*        it is an attribute item. */
  111.   COMMSEQ|         /*        also accessable from the keyboard. */
  112.   HIGHCOMP,        /*        complement the colours when highlihted. */
  113.   0x00000001,      /* MutualExclude, mutualexclude the first subitem. */
  114.   (APTR) &my_third_text, /* ItemFill, pointer to the text. */
  115.   NULL,            /* SelectFill, nothing since we complement the col. */
  116.   'U',             /* Command, the user can select this item by      */
  117.                    /*          pressing the right Amiga key together */
  118.                    /*          with the U key. */ 
  119.   NULL,            /* SubItem, ignored by Intuition. */
  120.   MENUNULL,        /* NextSelect, no items selected. */
  121. };
  122.  
  123.  
  124.  
  125. /*************************************************************************/
  126. /*                      S E C O N D   S U B I T E M                      */
  127. /*************************************************************************/
  128.  
  129. /* The text for the second subitem: */
  130. struct IntuiText my_second_text=
  131. {
  132.   2,          /* FrontPen, black. */
  133.   0,          /* BackPen, not used since JAM1. */
  134.   JAM1,       /* DrawMode, do not change the background. */
  135.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  136.               /* This will leave enough space for the check mark. */
  137.   1,          /* TopEdge, 1 line down. */
  138.   NULL,       /* TextAttr, default font. */
  139.   "Bold",     /* IText, the string. */
  140.   NULL        /* NextItem, no link to other IntuiText structures. */
  141. };
  142.  
  143. /* The MenuItem structure for the second subitem: */
  144. struct MenuItem my_second_subitem=
  145. {
  146.   &my_third_subitem, /* NextItem, linked to the third subitem. */
  147.   50,              /* LeftEdge, 50 pixels out. */
  148.   15,              /* TopEdge, 15 lines down. */
  149.   150,             /* Width, 150 pixels wide. */
  150.   10,              /* Height, 10 lines high. */
  151.   ITEMTEXT|        /* Flags, render this item with text. */
  152.   ITEMENABLED|     /*        this item will be enabled. */
  153.   CHECKIT|         /*        it is an attribute item. */
  154.   COMMSEQ|         /*        also accessable from the keyboard. */
  155.   HIGHCOMP,        /*        complement the colours when highlihted. */
  156.   0x00000001,      /* MutualExclude, mutualexclude the first subitem. */
  157.   (APTR) &my_second_text, /* ItemFill, pointer to the text. */
  158.   NULL,            /* SelectFill, nothing since we complement the col. */
  159.   'B',             /* Command, the user can select this item by      */
  160.                    /*          pressing the right Amiga key together */
  161.                    /*          with the B key. */ 
  162.   NULL,            /* SubItem, ignored by Intuition. */
  163.   MENUNULL,        /* NextSelect, no items selected. */
  164. };
  165.  
  166.  
  167.  
  168. /*************************************************************************/
  169. /*                       F I R S T   S U B I T E M                       */
  170. /*************************************************************************/
  171.  
  172. /* The text for the first subitem: */
  173. struct IntuiText my_first_text=
  174. {
  175.   2,          /* FrontPen, black. */
  176.   0,          /* BackPen, not used since JAM1. */
  177.   JAM1,       /* DrawMode, do not change the background. */
  178.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  179.               /* This will leave enough space for the check mark. */
  180.   1,          /* TopEdge, 1 line down. */
  181.   NULL,       /* TextAttr, default font. */
  182.   "Plain",    /* IText, the string. */
  183.   NULL        /* NextItem, no link to other IntuiText structures. */
  184. };
  185.  
  186. /* The MenuItem structure for the first subitem: */
  187. struct MenuItem my_first_subitem=
  188. {
  189.   &my_second_subitem, /* NextItem, linked to the second subitem. */
  190.   50,              /* LeftEdge, 50 pixels out. */
  191.   5,               /* TopEdge, 5 lines down. */
  192.   150,             /* Width, 150 pixels wide. */
  193.   10,              /* Height, 10 lines high. */
  194.   ITEMTEXT|        /* Flags, render this item with text. */
  195.   ITEMENABLED|     /*        this item will be enabled. */
  196.   CHECKIT|         /*        it is an attribute item. */
  197.   CHECKED|         /*        this item is initially selected. */
  198.   COMMSEQ|         /*        also accessable from the keyboard. */
  199.   HIGHCOMP,        /*        complement the colours when highlihted. */
  200.   0xFFFFFFFE,      /* MutualExclude, mutualexclude all items except the */
  201.                    /*                first one. */
  202.   (APTR) &my_first_text, /* ItemFill, pointer to the text. */
  203.   NULL,            /* SelectFill, nothing since we complement the col. */
  204.   'P',             /* Command, the user can select this item by      */
  205.                    /*          pressing the right Amiga key together */
  206.                    /*          with the P key. */ 
  207.   NULL,            /* SubItem, ignored by Intuition. */
  208.   MENUNULL,        /* NextSelect, no items selected. */
  209. };
  210.  
  211.  
  212.  
  213. /*************************************************************************/
  214. /*                       T H E   O N L Y   I T E M                       */
  215. /*************************************************************************/
  216.  
  217. /* The text for the item: */
  218. struct IntuiText my_text=
  219. {
  220.   2,          /* FrontPen, black. */
  221.   0,          /* BackPen, not used since JAM1. */
  222.   JAM1,       /* DrawMode, do not change the background. */
  223.   0,          /* LeftEdge, 0 pixels out. */
  224.               /* No space is needed for a check mark. */
  225.   1,          /* TopEdge, 1 line down. */
  226.   NULL,       /* TextAttr, default font. */
  227.   "Style",    /* IText, the string. */
  228.   NULL        /* NextItem, no link to other IntuiText structures. */
  229. };
  230.  
  231. /* The MenuItem structure for the item: */
  232. struct MenuItem my_item=
  233. {
  234.   NULL,              /* NextItem, no more items after this one. */
  235.   0,                 /* LeftEdge, 0 pixels out. */
  236.   0,                 /* TopEdge, 0 lines down. */
  237.   100,               /* Width, 100 pixels wide. */
  238.   10,                /* Height, 10 lines high. */
  239.   ITEMTEXT|          /* Flags, render this item with text. */
  240.   ITEMENABLED|       /*        this item will be enabled. */
  241.                      /*        it is an action item. (CHECKIT is not set) */
  242.   HIGHCOMP,          /*        complement the colours when highlihted. */
  243.   0,                 /* MutualExclude, no mutualexclude. */
  244.   (APTR) &my_text,   /* ItemFill, pointer to the text. */
  245.   NULL,              /* SelectFill, nothing since we complement the col. */
  246.   0,                 /* Command, no command-key sequence. */
  247.   &my_first_subitem, /* SubItem, pointer to the first subitem. */
  248.   MENUNULL,          /* NextSelect, no items selected. */
  249. };
  250.  
  251.  
  252.  
  253. /*************************************************************************/
  254. /*                              M E N U                                  */
  255. /*************************************************************************/
  256.  
  257. /* The Menu structure for the first (and only) menu: */
  258. struct Menu my_menu=
  259. {
  260.   NULL,        /* NextMenu, no more menu structures. */
  261.   0,           /* LeftEdge, left corner. */
  262.   0,           /* TopEdge, for the moment ignored by Intuition. */
  263.   50,          /* Width, 50 pixels wide. */
  264.   0,           /* Height, for the moment ignored by Intuition. */
  265.   MENUENABLED, /* Flags, this menu will be enabled. */
  266.   "Edit",      /* MenuName, the string. */
  267.   &my_item     /* FirstItem, pointer to the first (and only) item in */
  268.                /* the list. */
  269. };
  270.  
  271.  
  272.  
  273. /* Declare a pointer to a Window structure: */ 
  274. struct Window *my_window;
  275.  
  276. /* Declare and initialize your NewWindow structure: */
  277. struct NewWindow my_new_window=
  278. {
  279.   50,            /* LeftEdge    x position of the window. */
  280.   25,            /* TopEdge     y positio of the window. */
  281.   200,           /* Width       200 pixels wide. */
  282.   100,           /* Height      100 lines high. */
  283.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  284.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  285.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  286.                  /*             user has selected the Close window gad. */
  287.   MENUPICK,
  288.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  289.   WINDOWCLOSE|   /*             Close Gadget. */
  290.   WINDOWDRAG|    /*             Drag gadget. */
  291.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  292.   WINDOWSIZING|  /*             Sizing Gadget. */
  293.   ACTIVATE,      /*             The window should be Active when opened. */
  294.   NULL,          /* FirstGadget No Custom gadgets. */
  295.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  296.   "Style Editor",/* Title       Title of the window. */
  297.   NULL,          /* Screen      Connected to the Workbench Screen. */
  298.   NULL,          /* BitMap      No Custom BitMap. */
  299.   80,            /* MinWidth    We will not allow the window to become */
  300.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  301.   300,           /* MaxWidth    than 300 x 200. */
  302.   200,           /* MaxHeight */
  303.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  304. };
  305.  
  306.  
  307.  
  308. main()
  309. {
  310.   /* Boolean variable used for the while loop: */
  311.   BOOL close_me;
  312.  
  313.   /* Declare a variable in which we will store the IDCMP flag: */
  314.   ULONG class;
  315.   
  316.   /* If we recieve a MENUPICK event, the Code field of the message */
  317.   /* structure will contain the menu number of the first selected item. */
  318.   /* Declare a variable to store the Code value in, and an extra menu */
  319.   /* number variable: */
  320.   USHORT code, menu_number;
  321.   
  322.   /* Declare a MenuItem pointer: */
  323.   struct MenuItem *item;
  324.   
  325.   /* Declare a pointer to an IntuiMessage structure: */
  326.   struct IntuiMessage *my_message;
  327.  
  328.  
  329.  
  330.   /* Before we can use Intuition we need to open the Intuition Library: */
  331.   IntuitionBase = (struct IntuitionBase *)
  332.     OpenLibrary( "intuition.library", 0 );
  333.   
  334.   if( IntuitionBase == NULL )
  335.     exit(); /* Could NOT open the Intuition Library! */
  336.  
  337.  
  338.  
  339.   /* We will now try to open the window: */
  340.   my_window = (struct Window *) OpenWindow( &my_new_window );
  341.   
  342.   /* Have we opened the window succesfully? */
  343.   if(my_window == NULL)
  344.   {
  345.     /* Could NOT open the Window! */
  346.     
  347.     /* Close the Intuition Library since we have opened it: */
  348.     CloseLibrary( IntuitionBase );
  349.  
  350.     exit();  
  351.   }
  352.  
  353.  
  354.  
  355.   /* We have opened the window, and everything seems to be OK. */
  356.  
  357.  
  358.  
  359.   SetMenuStrip( my_window, &my_menu );
  360.   printf("Menustrip connected to window!\n");
  361.  
  362.  
  363.   close_me = FALSE;
  364.  
  365.   /* Stay in the while loop until the user has selected the Close window */
  366.   /* gadget: */
  367.   while( close_me == FALSE )
  368.   {
  369.     /* Wait until we have recieved a message: */
  370.     Wait( 1 << my_window->UserPort->mp_SigBit );
  371.  
  372.     /* As long as we collect messages sucessfully we stay in the loop: */
  373.     while(my_message=(struct IntuiMessage *) GetMsg( my_window->UserPort ))
  374.     {
  375.       /* After we have collected the message we can read it, and save any */
  376.       /* important values which we maybe want to check later: */
  377.       class = my_message->Class;
  378.       code = my_message->Code;
  379.  
  380.  
  381.       /* After we have read it we reply as fast as possible: */
  382.       /* REMEMBER! Do never try to read a message after you have replied! */
  383.       /* Some other process has maybe changed it. */
  384.       ReplyMsg( my_message );
  385.  
  386.       /* Check which IDCMP flag was sent: */
  387.       if( class == CLOSEWINDOW )
  388.         close_me=TRUE; /* The user selected the Close window gadget! */  
  389.  
  390.       if(class == MENUPICK)
  391.       {
  392.         printf("\nMenu pick!\n");
  393.         menu_number = code;
  394.         
  395.         while( menu_number != MENUNULL )
  396.         {
  397.           /* Get the address of the item: */
  398.           item = (struct MenuItem *) ItemAddress( &my_menu, menu_number );
  399.  
  400.  
  401.           /* Print out the menu number plus etc: */
  402.           printf("menu_number= %d\n", menu_number );
  403.           printf("MENUNUM = %d\n", MENUNUM(menu_number) );
  404.           printf("ITEMNUM = %d\n", ITEMNUM(menu_number) );
  405.           printf("SUBNUM  = %d\n", SUBNUM(menu_number) );
  406.  
  407.  
  408.           /* Get the following item's menu number: */
  409.           menu_number = item->NextSelect;
  410.         }
  411.       }
  412.     }
  413.   }
  414.  
  415.  
  416.  
  417.   printf("Menustrip removed from window!\n");
  418.   ClearMenuStrip( my_window );
  419.  
  420.  
  421.  
  422.   /* Close the window: */
  423.   CloseWindow( my_window );
  424.  
  425.  
  426.  
  427.   /* Close the Intuition Library since we have opened it: */
  428.   CloseLibrary( IntuitionBase );
  429.   
  430.   /* THE END */
  431. }
  432.